﻿using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Views;
using JiveMessenger.User;
using JiveMessenger.Utilities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace JiveMessenger.ViewModels
{
    public enum ImageCropType
    {
        Avatar,
        Emoticon
    }
   public abstract class ViewModelCropImageBase : ViewModelBaseEx
    {
        internal IRandomAccessStream _croppedData;
        internal IClient _currentClient;
        internal INavigationService _navigationService;
        public StorageFile OriginalImageFile { get; set; }


        public abstract RelayCommand SaveCommand { get; set; }
        public RelayCommand CropCommand { get; set; }
        public RelayCommand UndoCropCommand { get; set; }
        public RelayCommand CancelCommand { get; set; }


        private bool _showProgressOverlay;
        public bool ShowProgressOverlay
        {
            get { return _showProgressOverlay; }
            set
            {
                if (_showProgressOverlay != value)
                {
                    Set(() => ShowProgressOverlay, ref _showProgressOverlay, value);
                }
            }
        }


        private double _actualWidth;
        public double ActualWidth
        {
            get { return _actualWidth; }
            set
            {
                if (_actualWidth != value)
                {
                    Set(() => ActualWidth, ref _actualWidth, value);
                }
            }
        }
        private double _actualHeight;
        public double ActualHeight
        {
            get { return _actualHeight; }
            set
            {
                if (_actualHeight != value)
                {
                    Set(() => ActualHeight, ref _actualHeight, value);
                }
            }
        }

        private BitmapImage _avatarImageSource;
        public BitmapImage AvatarImageSource
        {
            get { return _avatarImageSource; }
            set
            {
                if (_avatarImageSource != value)
                {
                    Set(() => AvatarImageSource, ref _avatarImageSource, value);
                }
            }
        }



        private int _cropHeight;
        public int CropHeight
        {
            get { return _cropHeight; }
            set
            {
                if (_cropHeight != value)
                {
                    Set(() => CropHeight, ref _cropHeight, value);
                }
            }
        }

        private int _cropWidth;
        public int CropWidth
        {
            get { return _cropWidth; }
            set
            {
                if (_cropWidth != value)
                {
                    Set(() => CropWidth, ref _cropWidth, value);
                }
            }
        }

        private int _cropLeft;
        public int CropLeft
        {
            get { return _cropLeft; }
            set
            {
                if (_cropLeft != value)
                {
                    Set(() => CropLeft, ref _cropLeft, value);
                }
            }
        }

        private int _cropRight;
        public int CropRight
        {
            get { return _cropRight; }
            set
            {
                if (_cropRight != value)
                {
                    Set(() => CropRight, ref _cropRight, value);
                }
            }
        }

        private int _cropTop;
        public int CropTop
        {
            get { return _cropTop; }
            set
            {
                if (_cropTop != value)
                {
                    Set(() => CropTop, ref _cropTop, value);
                }
            }
        }

        private int _cropBottom;
        public int CropBottom
        {
            get { return _cropBottom; }
            set
            {
                if (_cropBottom != value)
                {
                    Set(() => CropBottom, ref _cropBottom, value);
                }
            }
        }
       public ViewModelCropImageBase(IClient currentClient, INavigationService navService)
       {
           _currentClient = currentClient;
           _navigationService = navService;

           CropCommand = new RelayCommand(CropAvatarCommandAction);
           UndoCropCommand = new RelayCommand(UndoCropCommandAction);
           CancelCommand = new RelayCommand(CancelCommandAction);
           CropHeight = 200;
           CropWidth = 200;
           CropLeft = 100;
           CropTop = 100;
           CropBottom = 200;
           CropRight = 300;
       }

        private void CancelCommandAction()
        {
            _navigationService.GoBack();
        }

        public async void SetOriginalImage(StorageFile file)
        {
            this.OriginalImageFile = file;
            this.AvatarImageSource = await StaticMethods.LoadBitmapImageFromIsolatedStorage(OriginalImageFile);
        }

        private async void UndoCropCommandAction()
        {
            this.AvatarImageSource = await StaticMethods.LoadBitmapImageFromIsolatedStorage(OriginalImageFile);
            if (_croppedData != null)
            {
                _croppedData.Dispose();
                _croppedData = null;
            }
        }

        private async void CropAvatarCommandAction()
        {
            ShowProgressOverlay = true;
            if (_croppedData == null)
            {
                var bitmap = await CropBitmap.GetCroppedBitmapAsync(OriginalImageFile, new Point(CropLeft, CropTop), new Size(CropRight - CropLeft, CropBottom - CropTop), 1);


                InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();

                // This is where the byteArray to be stored.
                var bytes = new byte[stream.Size];
                await bitmap.ToStreamAsJpeg(stream);
                await this.AvatarImageSource.SetSourceAsync(stream);
                //   AvatarImageSource = bitmap;
                _croppedData = stream;
                //await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, Windows.Storage.Streams.InputStreamOptions.None);


            }
            else
            {

                var bitmap = await CropBitmap.GetCroppedBitmapAsync(_croppedData, new Point(CropLeft, CropTop), new Size(CropRight - CropLeft, CropBottom - CropTop), 1);


                var stream = await SaveToStream(bitmap);
                await this.AvatarImageSource.SetSourceAsync(stream);
                //   AvatarImageSource = bitmap;
                _croppedData = stream;

            }
            ShowProgressOverlay = false;
        }
        internal async Task<IRandomAccessStream> SaveToStream(WriteableBitmap bitmap)
        {
            InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
            // This is where the byteArray to be stored.
            var bytes = new byte[stream.Size];
            await bitmap.ToStreamAsJpeg(stream);
            return stream;
        }
    }
}
